const App = {
    extends: Parent,
    methods: {
        init() {
            this.selectMyUser();
            //获取大厅全部动态
            //this.selectTrends();
            if(this.trends.length===0) this.selectTrends();
        },
        //创建社区
        createCommunity() {
            this.$confirm('确认创建社区？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var _this = this;
                axios.post(ParentUrl+"/Community/"+_this.communityName).then(resp=>{
                    _this.Result = resp.data;
                    if(_this.Result.code === 1) {
                        this.$message({
                            showClose: true,
                            message: '社区创建成功！',
                            type: 'success'
                        });
                        _this.createCommunityDialog=false;
                    } else {
                        this.$message({
                            showClose: true,
                            message: '社区创建失败！',
                            type: 'warning'
                        });
                    }
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消创建'
                });
            });
        },
        //查询动态
        selectTrends() {
            var _this = this;
            var num = _this.justLookFriend?2:1;
            var limit = _this.trends.length===0?-1:_this.trends.length-1;
            axios({
                method:"get",
                url:ParentUrl+"/Trends/"+num,
                params: {
                    limit: limit
                }
            }).then(resp => {
                _this.Result = resp.data;
                if(_this.Result.code === 1) {
                    //第一次查询直接赋值
                    if (_this.trends.length===0) {
                        _this.trends = _this.Result.data;
                    }
                    //查询数据与已有数据重复，不操作
                    else if(_this.trends[0].id===_this.Result.data[0].id) {
                    }
                    //后续查询，直接在底部添加
                    else {
                        _this.trends = _this.trends.concat(_this.Result.data);
                    }
                } else {

                }
            })
        },
        //发布动态
        publishTrend() {
            this.$confirm('确认该条动态？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var _this = this;
                _this.myTrends.content = $(".TrendsEditBox").html();
                axios({
                    method:"post",
                    url:ParentUrl+"/Trends",
                    data:_this.myTrends
                }).then(resp => {
                    _this.Result = resp.data;
                    if(_this.Result.code===1) {
                        _this.myTrends.content='';
                        _this.postUpdatesDrawer = false;
                        this.selectTrends();
                        this.$message({
                            showClose: true,
                            message: '动态发布成功！',
                            type: 'success'
                        });
                    } else {
                        this.$message({
                            showClose: true,
                            message: '动态发布失败！',
                            type: 'warning'
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //查询评论
        selectCritique(trendsId) {
            var _this = this;
            axios.get(ParentUrl+"/Critique/"+trendsId).then(resp=>{
                _this.Result = resp.data;
                if(_this.Result.code===1) {
                    _this.critiques = _this.Result.data;
                }
                else {
                    _this.critiques = null;
                }
            });
        },
        //查询社区
        selectCommunity(name) {
            var _this = this;
            axios.get(ParentUrl+"/Community/"+name).then(resp=>{
                _this.Result = resp.data;
                if(_this.Result.code===1) {
                    _this.hCommunity = _this.Result.data;
                }
                else {
                }
            });
        },
        //添加链接
        trendAddLink() {
            this.$prompt('请输入地址', '添加链接', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(({ value }) => {
                $(".TrendsEditBox").append("<button style=\"width: 100%;margin-left: 0%;margin-top: 1%;font-family: inherit;font-size: 13px;" +
                    "                    background: #409eff;color: white;padding: 0.7em 1em;padding-left: 0.9em;" +
                    "                    display: flex;align-items: center;border: none;border-radius: 10px;transition: all 0.2s;\" " +
                    "              onclick=\"window.open('"+value+"')\">" +
                    "        <svg style=\"display: block;transform-origin: center center;transition: transform 0.3s ease-in-out;\" " +
                    "             height=\"24\" width=\"24\" viewBox=\"0 0 24 24\" xmlns=\"http://www.w3.org/2000/svg\">" +
                    "          <path d=\"M0 0h24v24H0z\" fill=\"none\"></path>" +
                    "          <path d=\"M1.946 9.315c-.522-.174-.527-.455.01-.634l19.087-6.362c.529-.176.832.12.684.638l-5.454 19.086c-.15.529-.455.547-.679.045L12 14l6-8-8 6-8.054-2.685z\" fill=\"currentColor\"></path>" +
                    "        </svg>" +
                    "        <span style=\"display: block;margin-left: 0.3em;transition: all 0.3s ease-in-out;\">链接："+value+"</span>" +
                    "      </button><div>&ensp;</div>");
                this.$message({
                    type: 'success',
                    message: '添加成功'
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消输入'
                });
            });
        },
        //添加标签
        trendAddCommunity() {
            var _this = this;
            this.$prompt('请输入社区名称', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
            }).then(({ value }) => {
                axios.get(ParentUrl+"/Community/"+value).then(resp=>{
                    _this.Result = resp.data;
                    if(_this.Result.code === 1) {
                        this.$message({
                            showClose: true,
                            message: '添加成功！',
                            type: 'success'
                        });
                        this.myTrends.comTags[this.myTrends.comTags.length]=value;
                    } else {
                        this.$message({
                            showClose: true,
                            message: '社区不存在！',
                            type: 'warning'
                        });
                    };
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消输入'
                });
            });
        },
        //点赞
        like(trend) {
            var _this = this;
            axios.put(ParentUrl+"/Trends/1/"+trend.id).then(resp=>{
                _this.Result = resp.data;
                //成功点赞
                if (_this.Result.code===1) {
                    this.$message({
                        message: '点赞成功！',
                        type: 'success'
                    });
                    trend.likesNum++;
                }
                //取消点赞
                else if (_this.Result.code===-1) {
                    this.$message({
                        message: '已取消点赞。',
                        type: 'success'
                    });
                    trend.likesNum--;
                }
                //点赞失败
                else {
                    this.$message({
                        message: '点赞失败！',
                        type: 'warning'
                    });
                }
            });
        },
        //收藏
        collect(trend) {
            var _this = this;
            axios.put(ParentUrl+"/Trends/2/"+trend.id).then(resp=>{
                _this.Result = resp.data;
                //成功点赞
                if (_this.Result.code===1) {
                    this.$message({
                        message: '收藏成功！',
                        type: 'success'
                    });
                    trend.collectNum++;
                }
                //取消点赞
                else if (_this.Result.code===-1) {
                    this.$message({
                        message: '已取消收藏。',
                        type: 'success'
                    });
                    trend.collectNum--;
                }
                //点赞失败
                else {
                    this.$message({
                        message: '收藏失败！',
                        type: 'warning'
                    });
                }
            });
        },
        //评论
        critiqueTrends() {
            var _this = this;
            axios.post(ParentUrl+"/Critique", _this.critique).then(resp=>{
                _this.Result = resp.data;
                if(_this.Result.code===1) {
                    _this.critiques=null;
                    _this.critiqueDrawer = false;
                    this.$message({
                        showClose: true,
                        message: '评论成功！',
                        type: 'success'
                    });
                } else {
                    this.$message({
                        showClose: true,
                        message: '评论失败！',
                        type: 'warning'
                    });
                }
            })
        },
        yd1() {
            document.getElementById("content").style.transform="translate(0vw)";
        },
        yd2() {
            document.getElementById("content").style.transform="translate(100vw)";
        }
    },
    data() {
        return {
            activeIndex: '3',//标题栏高显位置
            createCommunityDrawer: false,//创建社区
            postUpdatesDrawer: false,//发布动态
            critiqueDrawer: false,//评论
            trendsDrawer: false,//查看全部
            userInformationDialog: false,//查看用户
            communityInformationDialog: false,//查看社区
            //用户发布动态
            myTrends: {
                title: '',
                content: '',
                comTags: [],//社区标签
                time:date.toLocaleDateString()+'/'+date.toLocaleTimeString(),
            },
            //需要添加的链接
            aContent: '',
            //编辑的动态
            myContent: '',
            //动态
            trends: [],
            //查看全部动态
            hTrend: {},
            hCommunity: {},//查看社区
            //动态评论
            critiques: [],
            //当前评论
            critique: {
                content: '',
                time: date.toLocaleDateString()+'/'+date.toLocaleTimeString(),
                trendsId: ''
            },
            communityName: '', //创建社区名称
            justLookFriend: false//只看好友
        };
    },
};
const app = Vue.createApp(App);
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
app.use(ElementPlus);
app.mount("#app");